package com.jida.tijian.service;

import com.jida.tijian.domain.Checkitem;
import com.jida.tijian.domain.Setmeal;
import com.jida.tijian.domain.Setmealdetailed;
import com.jida.tijian.dto.ResponseObject;
import com.jida.tijian.mapper.CheckitemMapper;
import com.jida.tijian.mapper.SetmealMapper;
import com.jida.tijian.mapper.SetmealdetailedMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired//拿到sql
    SetmealMapper setmealMapper;

    @Autowired
    SetmealdetailedMapper setmealdetailedMapper;

    //创建ci表(Mapper)接口
    @Autowired
    CheckitemMapper checkitemMapper;

    @Override
    public ResponseObject listSetmeal(Integer sex) {
        //看看参数有没有问题
        System.out.println("sex从控制层调用业务层的listSetmeal方法传参:" + "\n" + "service得到参数" + sex);
        //1.根据性别查询 性别有啥套餐
        //先停下来 向前端返回 验证一下结果如何
/*
        return new ResponseObject<>(
                ResponseObject.SUCCESS_STATUS,
                ResponseObject.SUCCESS_DESC,
                //调用数据库的selectAll方法
                setmealMapper.selectBySex(sex)
        );
        //上述return 是测试 selectBySex是否成功
 */
        List<Setmeal> setmealList = setmealMapper.selectBySex(sex);//这是一个list 有长度 大小
        //2。拿着套餐编号smId【1，2，3】 -> 查询有哪些小项目
        //2.1 首先根据 smIdList -> ciIdList
        for (int i = 0; i < setmealList.size(); i++) {
            Setmeal setmeal = setmealList.get(i);
            System.out.println("这是setmealList中下标为（" + i + ")的smId:" + setmeal.getSmId());//setmealList.get(i)  -> 拿到下标为 i 的对象-------> 再用过 getSmId 拿到对应的smId
            //创建一个有关setmealdetailed表的一个sql方法 可以根据 smId为参数 select 到对应的 ciId 返回类型为list
            List<Setmealdetailed> setmealdetailedList = setmealdetailedMapper.select_ciIds_By_smId(
                    setmeal.getSmId()//作为参数 -> 方法：select_ciId_By_smId
            );
            //容器
            List<Checkitem> checkitemList = new ArrayList<>();
//            System.out.println("执行了select_ciIds_By_smId\n" + "得到ciIds");

            for (int j = 0; j < setmealdetailedList.size(); j++) {
//                System.out.println("smID：" + setmealList.get(i).getSmId() +
//                        "，的ciId：" + setmealdetailedList.get(j).getCiId());

                //2.2 根据ciId -> * from cheackitemdetailed(ci表)
                Integer ciId = setmealdetailedList.get(j).getCiId();
                Checkitem checkitem = checkitemMapper.selectByPrimaryKey(ciId);
//                System.out.println(checkitem.getCiName());
                checkitemList.add(checkitem);
                setmeal.setCheckitemList(checkitemList);
            }
            System.out.println("========================================");
            for (int k = 0; k < setmeal.getCheckitemList().size(); k++) {
                System.out.println("打印setmeal中的属性CheakitemList中下标为：" + k + "的：（CiName）" + setmeal.getCheckitemList().get(k).getCiName());
            }
        }


        //default
//        return null;
        return new ResponseObject(
                ResponseObject.SUCCESS_STATUS,
                ResponseObject.SUCCESS_DESC,
                setmealList//为什么返回的是setmealList？？
        );
    }
}

